#include "BondDetailPrimary.h"
#include "BondDetailData.h"
#include "BondDetailTools.h"
#include "../QbMessageProcess.h"
#include <QVBoxLayout>
#include <QTableWidget>
#include <qbkernel/include/qb_base.h>
#include <core/StyleMgr.h>
#include <core/ImageMgr.h>
#include <QLabel>
#include <QPixmap>
#include <QEvent>
#include <QStylePainter>
#include <widgets/SImageButton.h>
#include <uam/UserAccountManager.h>
#include <QMessageBox>

//一级券
BondDetailPrimary::BondDetailPrimary(QWidget* p, QSharedPointer<DetailData> data)
	: QWidget(p), m_data(data)
{
	QHBoxLayout* h = new QHBoxLayout(this);
	h->setContentsMargins(4, 2, 6, 2);
	h->setSpacing(3);

	QVBoxLayout* l = new QVBoxLayout();
	l->setContentsMargins(0, 0, 0, 0);

	m_prcArea = new BondDetailPrcArea(this, m_data);
	l->addWidget(m_prcArea);

	m_prcInstitute = new BondDetailPrcInstitute(this, m_data);
	l->addWidget(m_prcInstitute);

	l->addSpacing(7);

	m_prcChange = new BondDetailPrcChange(this, m_data);
	l->addWidget(m_prcChange);

	l->addSpacing(1);

	m_prcMyPurchase = new BondDetailPrcMyPurchase(this, m_data);
	l->addWidget(m_prcMyPurchase);
	h->addLayout(l);

	QVBoxLayout* lLayout = new QVBoxLayout();
	lLayout->setContentsMargins(0, 4, 0, 0);

	m_prcRelevance = new BondDetailPrcRelevance(this, m_data);
	lLayout->addWidget(m_prcRelevance);
	lLayout->addSpacing(4);

	m_prcBondAprismt = new BondDetailPrcAprismt(this, m_data);
	lLayout->addWidget(m_prcBondAprismt);
	h->addLayout(lLayout);
	
	connect(m_data.get(), &DetailData::bondChanged, this, &BondDetailPrimary::onBondChanged);
	connect(m_prcRelevance, &BondDetailPrcRelevance::loadTabFinish, this, &BondDetailPrimary::onCreateConnect);
}

void BondDetailPrimary::onBondChanged()
{
	QB_CHECK_RETURN_VOID1(m_data && m_prcArea && m_prcInstitute && m_prcChange && m_prcRelevance && m_prcBondAprismt);
	m_prcArea->setBondInfo(m_data);
	m_prcRelevance->setBondInfo(m_data);
}

void BondDetailPrimary::onNotifyApristChart(std::string key)
{
	if (!m_bBondAprismtInit)
	{
		m_bBondAprismtInit = true;
		if (m_prcBondAprismt)
		{
			m_prcBondAprismt->setReqBondKey(key);
		}
	}
	else
	{
		if (m_prcBondAprismt)
			m_prcBondAprismt->setReqBondKey(key);
	}
}

void BondDetailPrimary::onCreateConnect()
{
	if (!m_bConnect)
	{
		connect(m_prcRelevance->getFirstRelevance(), &BondDetailPrcRelevanceFilter::filterBondSelected, this, &BondDetailPrimary::onNotifyApristChart);
		connect(m_prcRelevance->getSecondRelevance(), &BondDetailPrcRelevanceFilter::filterBondSelected, this, &BondDetailPrimary::onNotifyApristChart);
		connect(m_prcRelevance->getIssureRelevance(), &BondDetailPrcRelevanceFilter::filterBondSelected, this, &BondDetailPrimary::onNotifyApristChart);
	}
	m_bConnect = true;
}
